package com.wxdc.nmgyt.nmgytwydc.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.wxdc.nmgyt.nmgytwydc.R;

import java.io.File;
import java.util.List;

import static com.bumptech.glide.request.target.Target.SIZE_ORIGINAL;

/**
 * Created by Administrator on 2018/6/5.
 */

public class PhotoData_RecyclerViewAdapter extends RecyclerView.Adapter<PhotoData_RecyclerViewAdapter.myHolder> {
    private Context mContext;
    private List<String> urls;
    private int screenWidth,screenHeight;
    private int width;
    private int height;

    public PhotoData_RecyclerViewAdapter(Context context, List<String> urls) {
        mContext = context;
        this.urls = urls;
        screenWidth = mContext.getResources().getDisplayMetrics().widthPixels;//屏幕的宽度(px值）
        screenHeight = mContext.getResources().getDisplayMetrics().heightPixels;
        width = screenWidth / 3;//Item的宽度，或图片的宽度
        height = screenHeight / 3;
    }

    @NonNull
    @Override
    public PhotoData_RecyclerViewAdapter.myHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.activity_photo_item, parent, false);
        return new myHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull myHolder holder, int position) {
        String url = urls.get(position);
        holder.mImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mViewListener != null) {
                    mViewListener.onClickListener(position);
                }
            }
        });
        holder.onBind(url);
    }

    @Override
    public int getItemCount() {
        return urls == null ? 0 : urls.size();
    }

    //-------------------------------------------------------------------------------------------
    class myHolder extends RecyclerView.ViewHolder {
        private ImageView mImageView;

        public myHolder(View itemView) {
            super(itemView);
            mImageView = itemView.findViewById(R.id.photo_item_img);
        }

        public void onBind(String url) {
            File file = new File(url);// 加载本地图片
            Glide.with(mContext)
                    .load(file).asBitmap()
                    .diskCacheStrategy(DiskCacheStrategy.NONE)//这里设置了不做任何缓存
                    //.placeholder(R.drawable.plugin_camera_no_pictures)
                    //.override(width, SIZE_ORIGINAL)//这里的SIZE_ORIGINAL为Gilde里自带的参数
                    .override(width, height)//这里的SIZE_ORIGINAL为Gilde里自带的参数
                    .fitCenter()//缩放图像测量出来等于或小于ImageView的边界范围,该图像将会完全显示
                    .into(mImageView);
        }

    }

    //------------------------------------------------------------------------------------------
    private PhotoDataRecyclerViewListener mViewListener;
    public interface  PhotoDataRecyclerViewListener{
        void onClickListener(int position);
    }
    public void setItemListener(PhotoDataRecyclerViewListener listener){
        mViewListener = listener;
    }
}
